﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ICDA.Models.Entity;
using ICDA.Models;
using ICDA.Repositories.ICDA;
using ICDA.Web.Models;
using ICDA.Web.Helper;

namespace ICDA.Web.Controllers
{
    public class AdminController : Controller
    {
        IRepository _repository;
        private static AdminViewModel _adminModel;
        DatetTimeHelper dtHelper = new DatetTimeHelper();
        //
        // GET: /AdminViewModel/

        public AdminController()
            : this(new Repository())
        {
        }

        public AdminController(IRepository repository)
        {
            _repository = repository;
        }
        public static AdminViewModel adminModel
        {
            get
            {
                if (_adminModel == null)
                {
                    _adminModel = new AdminViewModel();
                }
                return _adminModel;
            }
        }
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Admin()
        {
            if (Session["UID"] != null)
            {
                ViewBag.Message = "Welcome to Kemper Team";
                return View();
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }

        public ActionResult LeaveAccount()
        {
            if (!string.IsNullOrEmpty(Session["UID"].ToString()) && string.Equals(Session["Role"].ToString().Trim(), "Admin"))
            {
                adminModel.CurrentDateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                adminModel.LeaveLayout.Clear();
                DatetTimeHelper dtHelp = new DatetTimeHelper(adminModel.CurrentDateTime);
                foreach (UserTable user in _repository.users.ToList())
                {
                    List<LeaveTable> lTable = _repository.leavetable.Where(x => string.Equals(x.UserID, user.UserID) 
                        && x.LeaveStartDate >= dtHelp.MonthStartDate
                        && x.LeaveEndDate <= dtHelp.MonthEndDate).ToList();
                    List<string> color = new List<string>(dtHelp.TotalDaysInMonth);

                    for (int i = 0; i < color.Capacity; i++)
                    {
                        color.Add("#E3E3E4");
                    }
                    if (lTable != null && lTable.Count > 0)
                    {
                        lTable.ForEach
                             ( lt => 
                                {
                                    for (int i = lt.LeaveStartDate.Day -1 ; i <= lt.LeaveEndDate.Day -1; i++)
                                    {
                                        if (string.Equals(lt.LeaveType.Trim(), "Leave"))
                                        {
                                            color[i] = lt.DayType == null ? "#E01B35" : string.Equals(lt.DayType.Trim(), "Full Day") ? "#E01B35" : "#6BBC4E";
                                        }
                                        else if (string.Equals(lt.LeaveType.Trim(), "Training"))
                                        {
                                            color[i] = lt.DayType == null ? "#5E75E9" : string.Equals(lt.DayType.Trim(), "Full Day") ? "#5E75E9" : "#E9BD5E";
                                        }
                                    }
                           });
                    }
                    adminModel.LeaveLayout.Add(user.Name.Trim(), color);
                }
                return View(adminModel);
            }
            else
            {
                return View();
            }
        }

        [HttpGet]
        public ActionResult LeaveByMonth(DateTime id)
        {
            if (!string.IsNullOrEmpty(Session["UID"].ToString()) && string.Equals(Session["Role"].ToString().Trim(), "Admin"))
            {
                AdminViewModel adminM = new Models.AdminViewModel();
                adminM.CurrentDateTime = new DateTime(id.Year, id.Month, id.Day);
                DatetTimeHelper dtHelp = new DatetTimeHelper(adminM.CurrentDateTime);
                adminM.LeaveLayout.Clear();
                foreach (UserTable user in _repository.users.ToList())
                {
                    List<LeaveTable> lTable = _repository.leavetable.Where(x => string.Equals(x.UserID, user.UserID) && x.LeaveStartDate >= dtHelp.MonthStartDate && x.LeaveEndDate <= dtHelp.MonthEndDate).ToList();
                    List<string> color = new List<string>(dtHelp.TotalDaysInMonth);
                    for (int i = 0; i < color.Capacity; i++)
                    {
                        color.Add("#E3E3E4");
                    }
                    if (lTable != null && lTable.Count > 0)
                    {
                    lTable.ForEach
                            (lt =>
                            {
                                for (int i = lt.LeaveStartDate.Day - 1; i <= lt.LeaveEndDate.Day - 1; i++)
                                {
                                    if (string.Equals(lt.LeaveType.Trim(), "Leave"))
                                    {
                                        color[i] = lt.DayType == null ? "#E01B35" : string.Equals(lt.DayType.Trim(), "Full Day") ? "#E01B35" : "#6BBC4E";
                                    }
                                    else if (string.Equals(lt.LeaveType.Trim(), "Training"))
                                    {
                                        color[i] = lt.DayType == null ? "#5E75E9" : string.Equals(lt.DayType.Trim(), "Full Day") ? "#5E75E9" : "#E9BD5E";
                                    }
                                }
                            });
                        }
                    adminM.LeaveLayout.Add(user.Name.Trim(), color);
                }
                return View("LeaveAccount", adminM);
            }
            else
            {
                return View();
            }
        }
        public ActionResult UserAccount()
        {
            if (!string.IsNullOrEmpty(Session["UID"].ToString()) && string.Equals(Session["Role"].ToString().Trim(), "Admin"))
            {

                return View();
            }
            else
            {
                return View();
            }
        }

        public ActionResult UserActivity()
        {
            if (!string.IsNullOrEmpty(Session["UID"].ToString()) && string.Equals(Session["Role"].ToString().Trim(), "Admin"))
            {
                AdminViewModel adminM = new Models.AdminViewModel();
                adminM.CurrentDateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                DatetTimeHelper dtHelp = new DatetTimeHelper(adminM.CurrentDateTime);
                foreach (UserTable ut in _repository.users)
                {
                    var activityTable = _repository.activity.Where(u => u.UserID == ut.UserID && u.ActivityDate >= dtHelp.MonthStartDate && u.ActivityDate <= dtHelper.MonthEndDate);
                    List<ActivityViewModel> actvityList = new List<ActivityViewModel>();

                    foreach (var items in activityTable)
                    {
                        ActivityViewModel avModel = new ActivityViewModel();
                        avModel.ID = items.ID;
                        avModel.ActivityDate = items.ActivityDate;
                        avModel.OverTime =Convert.ToInt32(items.OverTime);
                        avModel.TimeSpend = items.TimeSpend;
                        avModel.Task = items.Task;
                        avModel.UserID = ut.UserID;
                        avModel.UserName = ut.Name;
                        
                        actvityList.Add(avModel);
                    }

                    adminM.ActivityLayout.Add(ut.Name.Trim(), actvityList);
                }
                
                return View(adminM);
            }
            else
            {
                return View();
            }
        }
        public ActionResult AddUser()
        {
            return View();
        }
    }
}
